<?php
set_time_limit(0);
class PageController extends Controller {

    public function actionIndex() {
        
        $sql = "SELECT * FROM site_slider ORDER BY slider_order_by ASC";
        $data = Yii::app()->db->createCommand($sql)->queryAll();
        
        $this->render('index', array(
            'data' => $data,
        ));
    }

    public function actionShow() {
        
        $id = (isset($_GET['id'])) ? $_GET['id'] : 1;
        $page_model = site_page::model()->findByPk($id);
        
        $this->render('page', array('page_model' => $page_model));
    }
    
    public function actionOnline()
    {
        $this->widget('application.extensions.online_payment.online_payment');
        
    }
     public function actionRun() {
        $data_arr = array('sponsor', 'match', 'extra_sponsor', 'gen_node', 'gen_node_extra', 'gen_match', 'matrix_node', 'board_fly', 'board_level');
        //$data_arr = array('gen_node');
        //ambil semua data network-nya,,..
        //$network = sys_network::model()->findAll();
        $network = Yii::app()->db->createCommand("SELECT network_id FROM sys_network")->queryAll();
        foreach($network as $row) {
            //ambil jumlah titik kanan dan kirinya
            $node = Yii::app()->db->createCommand("SELECT SUM(netgrow_master_node_left) as total_left, SUM(netgrow_master_node_right) as total_right
                                                   FROM sys_netgrow_master WHERE netgrow_master_network_id = '".$row['network_id']."'")->queryRow();
            $titik_kiri = isset($node['total_left'])?$node['total_left']:0;
            $titik_kanan = isset($node['total_right'])?$node['total_right']:0;
            //update ke sys_network-nya
            Yii::app()->db->createCommand("UPDATE sys_network SET network_total_node_left = ".$titik_kiri.", network_total_node_right = ".$titik_kanan."
                                           WHERE network_id = '".$row['network_id']."'")->execute();
            //ambil semua networknya kemudian di update ke sys bonus log
            foreach($data_arr as $row_bonus) {
                $this->update_bonus($row_bonus, $row['network_id']);
            }
        }
    }
    /**
     * Method buat update bonus 
     * @param String $bonus
     * @param int $network_id
     */
    public function update_bonus($bonus, $network_id) {
        //ambil bonus-nya dari bonus log
        $sql_bonus = "SELECT SUM(".$bonus."_bonus_log_value) as bonus FROM sp_".$bonus."_bonus_log WHERE ".$bonus."_bonus_log_network_id = '".$network_id."' LIMIT 1";
        $bonus_table = Yii::app()->db->createCommand($sql_bonus)->queryColumn();
        $bonus_table['bonus'] = intval($bonus_table[0]);
        $total_bonus = isset($bonus_table['bonus'])?$bonus_table['bonus']:0;
        //update ke sys bonusnya sekarang
        $sql_update = "UPDATE sp_".$bonus."_bonus SET ".$bonus."_bonus_acc = ".$total_bonus." 
                       WHERE ".$bonus."_bonus_network_id = '".$network_id."' ORDER BY ".$bonus."_bonus_network_id ASC LIMIT 1";
        echo "<pre>";
        echo $sql_bonus;
        echo $total_bonus;
        print_r($bonus_table);
        if($bonus == 'board_fly') {
            die();
        }
        Yii::app()->db->createCommand($sql_update)->execute();
    }
}
